﻿using gAMS_Sacombank.Web.Services.AssetManagement.Functions;
using gAMS_Sacombank.Web.Services.AssetManagement.Implement;
using gAMS_Sacombank.Web.Services.AssetManagement.Interfaces;
using gAMS_Sacombank.Web.Services.AssetManagement.Interfaces.AssetManager;

using gAMS_Sacombank.Web.Services.AssetManagement.Interfaces.Trade;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Text;
using System.Transactions;
using System.Windows.Forms;
using System.Xml.Linq;

namespace gAMS_Sacombank.Web.Services.AssetManagement
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    [AspNetCompatibilityRequirements(
        RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class AssetService : ImplementInterface, IUnit         
    {
       

        #region Unit

        public IEnumerable<CM_UNIT> GetByAllUnit()
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = dataContext.CM_UNITs.ToList();
                    return result;
                }

            }
            catch (Exception)
            {
                return null;
            }
        }

        public string InsertUnit(CM_UNIT data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    //Check existed
                    var existItem = (from item in dataContext.CM_UNITs where item.UNIT_CODE.Equals(data.UNIT_CODE) select item).FirstOrDefault();
                    if (existItem != null) return ListMessage.Existed;

                    //Phat sinh ma
                    var result = (from item in dataContext.CM_UNITs                                  
                                  orderby item.UNIT_ID descending
                                  select item).FirstOrDefault();
                    if (result != null)
                        data.UNIT_ID = (int.Parse(result.UNIT_ID.Replace("DV", "")) + 1).ToString("DV000");
                    else
                        data.UNIT_ID = "DV001";

                    dataContext.CM_UNITs.InsertOnSubmit(data);
                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string UpdateUnit(CM_UNIT data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = (from item in dataContext.CM_UNITs
                                  where item.UNIT_CODE.Equals(data.UNIT_CODE)
                                  select item).SingleOrDefault();
                    if (result == null) return ListMessage.NotExisted;

                    MyHelper.Copy(result, data);

                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string DeleteUnit(string id)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = (from item in dataContext.CM_UNITs
                                  where item.UNIT_CODE.Equals(id)
                                  select item).SingleOrDefault();
                    if (result == null) return ListMessage.NotExisted;

                    dataContext.CM_UNITs.DeleteOnSubmit(result);
                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string ApproveUnit(CM_UNIT data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = (from item in dataContext.CM_UNITs
                                  where item.UNIT_CODE.Equals(data.UNIT_CODE)
                                  select item).SingleOrDefault();
                    if (result == null) return ListMessage.NotExisted;

                    result.AUTH_STATUS = "A";
                    result.CHECKER_ID = data.CHECKER_ID;
                    result.APPROVE_DT = data.APPROVE_DT;

                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public IEnumerable<CM_UNIT> GetByTopUnit(string _top, string _where, string _order)
        {
            try
            {
                using (var data = new AssetDataContext())
                {
                    string query = "Select * from [CM_UNIT]";
                    if (!_top.Equals(""))
                        query = " Select top " + _top + " * from [CM_UNIT] ";
                    if (!_where.Equals(""))
                        query += " where " + _where + " ";
                    if (!_order.Equals(""))
                        query += " order by " + _order;

                    IEnumerable<CM_UNIT> results = data.ExecuteQuery<CM_UNIT>(@query);

                    return results.ToList();
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }

        #endregion

       
    }   
}